/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.controller;

import framework.model.Account;
import framework.model.Entry;
import framework.model.Factory;
import framework.model.FinancialCompany;
import framework.view.EntryDialog;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author ThanhNguyen
 */
public class DepositController implements IController {

    protected String depositText;

    public DepositController(String depositText) {
        this.depositText = depositText;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        FinancialCompany financialCompany = Factory.getInstance().getFinancialCompany();
        JTable table = financialCompany.getUI().getTable();
        int selectedRow = table.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(null, "Please select a row first.");
            return;
        }
        Account account = financialCompany.getAccounts().get(selectedRow);
        EntryDialog d = new EntryDialog(depositText, account.getCustomer().getName());
        d.setVisible(true);
        if (d.isOK()) {
            Entry entry = Factory.getInstance().createEntry(d.getAmount());
            ITransaction transaction = Factory.getInstance().createTransaction(account, entry, ITransaction.T_DEPOSIT);
            TransactionManager.getInstance().storeTransaction(transaction);
            financialCompany.updateUI();
        }
    }
}
